/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DataAccess;

import DomainModel.Course;
import DomainModel.ResetPassword;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 *
 * @author Alex
 */
public class ResetPasswordDA {

    private Connection conn;
    private PreparedStatement prep;

    public ResetPasswordDA() throws Exception {
        conn = DBManager.getInstance().getConnection();
    }

    ResetPassword getResetPasswordByToken(String token) throws Exception {
        ResetPassword resetPassword = null;
        String q = "SELECT * FROM reset_password WHERE token = ?";
        prep = conn.prepareStatement(q);
        prep.setString(1, token);

        ResultSet rs = prep.executeQuery();

        try {
            if (rs.next()) { //if the user was found
                resetPassword = this.createResetPassword(rs);
            }
            return resetPassword;
        } catch (java.sql.SQLException e) {
            String err_message = "getResetPasswordByToken: Error occured: "
                    + e.getMessage();
            System.out.println(err_message);
            throw e;
        } finally {
            prep.close();
        }
    }

    public void deleteResetPasswordByToken(String token) throws Exception {
        String q;

        q = "DELETE FROM reset_password WHERE token = ?";

        prep = conn.prepareStatement(q);
        prep.setString(1, token);

        try {
            prep.executeUpdate();
        } catch (SQLException e) {
            throw e;
        } finally {
            prep.close();
        }
    }

    public boolean generatePasswordResetForUser(int userId, String token, java.sql.Timestamp expire_date) throws Exception {
        String q;

        q = "INSERT INTO reset_password (user_id, token, expire_date) "
                + "VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE user_id = ?, token = ?, expire_date = ?";

        prep = conn.prepareStatement(q);
        prep.setInt(1, userId);
        prep.setString(2, token);
        prep.setTimestamp(3, expire_date);
        prep.setInt(4, userId);
        prep.setString(5, token);
        prep.setTimestamp(6, expire_date);

        try {
            prep.executeUpdate();
        } catch (SQLException e) {
            throw e;
        } finally {
            prep.close();
        }
        return true;
    }

    private ResetPassword createResetPassword(ResultSet rs) throws SQLException {
        if (rs == null) {
            return null;
        }

        int id = rs.getInt("id");
        int userId = rs.getInt("user_id");
        String token = rs.getString("token");
        Date expireDate = rs.getDate("expire_date");

        return new ResetPassword(id, userId, token, expireDate);
    }

}
